package com.mall.controller;

import com.mall.common.AuthCheck;
import com.mall.common.Result;
import com.mall.entity.Product;
import com.mall.service.ProductService;
import com.mall.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理端商品管理
 */
@RestController
@RequestMapping("/admin/product")
public class AdminProductController {

    @Autowired
    private ProductService productService;

    /**
     * 分页获取商品列表
     */
    @GetMapping("/list")
    @AuthCheck
    public Result<List<ProductVO>> listProducts(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword
    ) {
        // TODO: 实现分页和模糊查询
        List<ProductVO> list = productService.listProducts(pageNum, pageSize, keyword);
        return Result.success(list);
    }

    /**
     * 新增商品
     */
    @PostMapping
    @AuthCheck
    public Result<String> addProduct(@RequestBody Product product) {
        productService.addProduct(product);
        return Result.success("添加成功");
    }

    /**
     * 编辑商品
     */
    @PutMapping("/{id}")
    @AuthCheck
    public Result<String> updateProduct(@PathVariable Long id, @RequestBody Product product) {
        product.setId(id);
        productService.updateProduct(product);
        return Result.success("更新成功");
    }

    /**
     * 删除商品
     */
    @DeleteMapping("/{id}")
    @AuthCheck
    public Result<String> deleteProduct(@PathVariable Long id) {
        productService.deleteProduct(id);
        return Result.success("删除成功");
    }

    /**
     * 根据ID获取商品
     */
    @GetMapping("/{id}")
    @AuthCheck
    public Result<ProductVO> getProduct(@PathVariable Long id) {
        ProductVO product = productService.getProductVOById(id);
        return Result.success(product);
    }
}